package com.udian.frame.code;

import org.springframework.util.StringUtils;

import java.util.List;

public class Table {

	/**
	 * 表名前缀删除  逗号隔开
	 */
	private String prefix;

	/**
	 * 作者
	 */
	private String author;

	/**
	 * 创建日期
	 */
	private String date;

	/**
	 * 版本号
	 */
	private String version;

	/**
	 * 包名称
	 */
	private String packagePath;

	
	/**
	 * 项目名称
	 */
	private String projectName;
	
	/**
	 * 项目路径
	 */
	private String projectPath;

	/**
	 * 表名
	 */
	private String name;
	
	/**
	 * 说明
	 */
	private String comment;
	
	/**
	 * 字段信息
	 */
	private List<Column> columnList;
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Column> getColumnList() {
		return columnList;
	}

	public void setColumnList(List<Column> columnList) {
		this.columnList = columnList;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
	
	public String getAlias() {
		String alias = getAliasObject();
		alias =  alias.substring(0,1).toUpperCase() + alias.substring(1, alias.length());
		return alias;
	}

	public String getAliasObject() {
		String aliasObj = name.toLowerCase();
//		int index = aliasObj.indexOf("_");
//		if(index>0){
//			aliasObj = aliasObj.substring(index,aliasObj.length());
//		}

		if(!StringUtils.isEmpty(prefix)){
			String[] replaceArr = prefix.split(",");
			for(String  r : replaceArr){
				aliasObj = aliasObj.replace(r,"");
			}
		}

		return CamelUtil.underlineToCamel(aliasObj);
	}
	
	
	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}
	
	/**
	 * 
	 * @return
	 */
	public String getColumns() {
		StringBuilder sb = new StringBuilder();
		int index = 0;
		int length = columnList.size();
		for(Column column : columnList){
			sb.append(column.getName());
			if(++index<length){
				sb.append(", ");
			}
		}
		return sb.toString();
	}
	
	public String getProperties() {
		StringBuilder sb = new StringBuilder();
		int index = 0;
		int length = columnList.size();
		for(Column column : columnList){
			sb.append("#{"+column.getAlias()+"}");
			if(++index<length){
				sb.append(",");
			}
		}
		return sb.toString();
	}
	
	
	public String getResultMap() {
		StringBuilder sb = new StringBuilder();
		int index = 0;
		int length = columnList.size();
		for(Column column : columnList){
			sb.append(column.getName());
			if(!column.getName().equals(column.getAlias())){
				sb.append(" AS " + column.getAlias());
			}
			if(++index<length){
				sb.append(", ");
			}
		}
		return sb.toString();
	}

	public String getProjectPath() {
		return projectPath;
	}

	public void setProjectPath(String projectPath) {
		this.projectPath = projectPath;
	}


	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getPackagePath() {
		return packagePath;
	}

	public void setPackagePath(String packagePath) {
		this.packagePath = packagePath;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}
}
